<?php

namespace zhtfrontend\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%comment_info}}".
 *
 * @property integer $id
 * @property integer $object_type
 * @property integer $title
 * @property string $comment_type
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $last_user
 * @property integer $last_ut
 * @property string $comment_term_type
 */
class CommentInfo extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comment_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'object_type'], 'integer'],
            [['comment_type','title'], 'string', 'max' => 50],
            [['comment_term_type','object_type','comment_type','title'],'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'object_type' => Yii::t('zhtfrontend', '评价对象'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'comment_type' => Yii::t('zhtfrontend', '显示类型'),
            'user_id' => Yii::t('zhtfrontend', '管理员'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'last_user' => Yii::t('zhtfrontend', '修改人'),
            'last_ut' => Yii::t('zhtfrontend', '最后修改时间'),
            'comment_term_type' => Yii::t('zhtfrontend','评价项类型'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['object_type'] = function () {
            switch ($this->object_type) {
                case 1:
                    return \Yii::t('zhtfrontend', '老师');
                case 2:
                    return \Yii::t('zhtfrontend', '机构');
                case 3:
                    return \Yii::t('zhtfrontend', '员工');
                case 4:
                    return \Yii::t('zhtfrontend', '部门');
                case 5:
                    return \Yii::t('zhtfrontend', '广场高管');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['last_ut'] = function () {
            return \Yii::$app->formatter->asDatetime($this->last_ut);
        };
        $fields['user'] = function () {
            $user = Admin::findOne(['id'=>$this->user_id]);
            return \Yii::t('zhtfrontend', $user->name);
        };
        $fields['lastuser'] = function () {
            $user = Admin::findOne(['id'=>$this->last_user]);
            return \Yii::t('zhtfrontend', $user->name);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
            'user' => function () {
                $user = Admin::findOne(['id'=>$this->user_id]);
                return \Yii::t('zhtfrontend', $user->name);
            },
            'lastuser' => function () {
                $user = Admin::findOne(['id'=>$this->last_user]);
                $username = !empty($user)?$user->name:'--';
                return \Yii::t('zhtfrontend', $username);
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'commentinfo',
            ],
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
